package com.taptech.doufu.ui.viewholder.personalcenter;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.taptech.doufu.util.TTLog;
import com.taptech.doufu.R;
import com.taptech.doufu.ui.activity.weex.SimpleWeexActivity;
import com.taptech.doufu.bean.MineNovelBean;
import com.taptech.doufu.bean.MineNovelReviewBean;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.services.personalcenter.AccountService;
import com.taptech.doufu.ui.view.personalcenter.ContentTagsViewGroup;
import com.taptech.doufu.util.DiaobaoUtil;
import com.taptech.doufu.util.GlideUtil;
import com.taptech.doufu.util.StartActivityUtil;
import com.taptech.doufu.util.UIUtil;


public class NovelReviewMessageViewHolder extends BaseAdapterViewHolder implements OnClickListener, View.OnLongClickListener {
    // 总数据
    MineNovelReviewBean bean;
    MineNovelBean novelBean;

    // 进入详情的点击控件
    protected RelativeLayout itemSweepClickBtn;

    public TextView mTitleLevel;
    public TextView sweepTitle;
    public TextView sweepDes;
    public ContentTagsViewGroup sweepTaglist;
    public TextView sweepReadTimes;
    public TextView mUserName;
    public TextView mAuthorName;
    public TextView flowerCount;
    public TextView commentCount;
    public TextView isElite;
    public ImageView sweepReadTimeImg;
    public ImageView mUserImg;
    public ImageView mUserMedal;
    protected Bitmap defaultBitmap = null;
    protected Handler mHandler;
    protected TextView mDeleteBtn;
    protected TextView mCreateTime;
    protected TextView mCircleSource;

    public NovelReviewMessageViewHolder(Context context) {
        super(context, -1, R.layout.home_item_sweep_new);
        // TODO Auto-generated constructor stub
    }

    public NovelReviewMessageViewHolder(Context context, int position) {
        super(context, position, R.layout.home_item_sweep_new);
        // TODO Auto-generated constructor stub
    }

    public NovelReviewMessageViewHolder(Context context, int position, int layoutId) {
        super(context, position, layoutId);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void getChildView(View convertView) {

        itemSweepClickBtn = (RelativeLayout) convertView.findViewById(R.id.item_sweep_click_btn);
        sweepTitle = (TextView) convertView.findViewById(R.id.home_item_sweep_title);
        sweepDes = (TextView) convertView.findViewById(R.id.home_item_sweep_des);
        sweepTaglist = (ContentTagsViewGroup) convertView.findViewById(R.id.home_item_sweep_taglist);
        sweepReadTimeImg = (ImageView) convertView.findViewById(R.id.home_item_sweep_read_time_icon);
        sweepReadTimes = (TextView) convertView.findViewById(R.id.home_item_sweep_read_times);
        flowerCount = (TextView) convertView.findViewById(R.id.home_item_sweep_flower_count);
        commentCount = (TextView) convertView.findViewById(R.id.home_item_comment_count);
        mUserImg = (ImageView) convertView.findViewById(R.id.home_item_sweep_userimg);
        mUserName = (TextView) convertView.findViewById(R.id.home_item_sweep_username);
        mAuthorName = (TextView) convertView.findViewById(R.id.home_item_sweep_author);
        mTitleLevel = (TextView) convertView.findViewById(R.id.home_item_sweep_title_level);

        mUserMedal = (ImageView) convertView.findViewById(R.id.home_item_sweep_userimg_medal);
        mDeleteBtn = (TextView) convertView.findViewById(R.id.delete_item_btn);
        mCreateTime = (TextView) convertView.findViewById(R.id.home_item_sweep_create_time);
        mCircleSource = (TextView) convertView.findViewById(R.id.home_item_source);
    }

    @Override
    public void setChildView(Object object,int singlePage) {

        bean = (MineNovelReviewBean) object;

        if (bean == null) {
            return;
        }
        novelBean = bean.getNovel();
        if (novelBean == null) {
            return;
        }
        itemSweepClickBtn.setOnClickListener(this);
        itemSweepClickBtn.setOnLongClickListener(this);
        if (mDeleteBtn != null) {
            mDeleteBtn.setOnClickListener(this);
        }
        mUserImg.setOnClickListener(this);
        // 用户头像
        // 默认头像
        Drawable userBitmap;
        userBitmap = mContext.getResources().getDrawable(R.drawable.default_user_portrait);
        mUserImg.setImageDrawable(userBitmap);
        if (bean.getUser() != null) {
            GlideUtil.displayCircleImage(mUserImg, bean.getUser().getIcon());
        }
        // 文评星级显示
        if (bean.getStar() != null) {
            String level = UIUtil.showStarLevel(bean.getStar(), mTitleLevel);
            mTitleLevel.setText(level);
        }
        // 文评标题
        if (novelBean.getTitle() != null) {
            sweepTitle.setText(novelBean.getTitle());
        }
        // 用户名字
        if (bean.getUser() != null) {
            mUserName.setText(bean.getUser().getName());
        }
        // 作者名字
        if (novelBean.getAuthor() != null) {
            mAuthorName.setText("作者：" + novelBean.getAuthor());
        }
        // 标签信息
        if (bean.getTag_list() != null) {

            String[] tagStrings = bean.getTag_list().split("\\s+");
            sweepTaglist.setTextTags(mContext, tagStrings);
            sweepTaglist.setType(Constant.OBJECT_TYPE_SWEEP);
            TTLog.d("tag", "the tagslist length is===================" + tagStrings.length);
            TTLog.d("tag", "the tagslist  is===================" + bean.getTag_list());
        }
        if (bean.getReview() != null) {
            sweepDes.setText(bean.getReview());
        }
        // 被浏览数
        if (bean.getRead_num() != null) {
            sweepReadTimes.setText(bean.getRead_num());
        }
        // 被送花数
        if (bean.getFlower_num() != null) {
            flowerCount.setText(bean.getFlower_num());
        }
        // 被评论数
        if (bean.getComment_times() != null) {
            commentCount.setText(bean.getComment_times());
        }
        if (bean.getUser() != null && bean.getUser().getMedal() != null) {
            DiaobaoUtil.setMedalImgView(bean.getUser().getMedal(), mUserMedal);
        }
        if (bean.getUser() != null && AccountService.getInstance().isUserSelf(bean.getUser().getUserId()) && mHandler != null) {
            mDeleteBtn.setVisibility(View.VISIBLE);
        } else {
            mDeleteBtn.setVisibility(View.GONE);
        }

        if (bean != null && bean.getAdd_time() != null) {
            mCreateTime.setText(DiaobaoUtil.seconds2TimeString(Long.parseLong(bean.getAdd_time())));
        }
    }

    @Override
    public void setChildView(Object object, String keyWord,int singlePage) {
        bean = (MineNovelReviewBean) object;

        if (bean == null) {
            return;
        }
        novelBean = bean.getNovel();
        if (novelBean == null) {
            return;
        }
        itemSweepClickBtn.setOnClickListener(this);
        itemSweepClickBtn.setOnLongClickListener(this);
        if (mDeleteBtn != null) {
            mDeleteBtn.setOnClickListener(this);
        }
        mUserImg.setOnClickListener(this);
        // 用户头像
        // 默认头像
        Drawable userBitmap;
        userBitmap = mContext.getResources().getDrawable(R.drawable.default_user_portrait);
        mUserImg.setImageDrawable(userBitmap);
        if (bean.getUser() != null) {
            GlideUtil.displayCircleImage(mUserImg, bean.getUser().getIcon());
        }
        // 文评星级显示
        if (bean.getStar() != null) {
            String level = UIUtil.showStarLevel(bean.getStar(), mTitleLevel);
            mTitleLevel.setText(level);
        }
        // 文评标题
        if (novelBean.getTitle() != null) {
            sweepTitle.setText(DiaobaoUtil.textLighterColor(novelBean.getTitle() , keyWord));
        }
        // 用户名字
        if (bean.getUser() != null) {
            mUserName.setText(DiaobaoUtil.textLighterColor(bean.getUser().getName() , keyWord));
        }
        // 作者名字
        if (novelBean.getAuthor() != null) {
            mAuthorName.setText(DiaobaoUtil.textLighterColor("作者：" + novelBean.getAuthor() , keyWord));
        }
        // 标签信息
        if (bean.getTag_list() != null) {

            String[] tagStrings = bean.getTag_list().split("\\s+");
            sweepTaglist.setTextTags(mContext, tagStrings);
            sweepTaglist.lightChildTextView(keyWord);
            TTLog.d("tag", "the tagslist length is===================" + tagStrings.length);
            TTLog.d("tag", "the tagslist  is===================" + bean.getTag_list());
        }
        // 文评简介
        if (bean.getReview() != null) {
            sweepDes.setText(DiaobaoUtil.textLighterColor(bean.getReview() , keyWord) );
        }
        // 被浏览数
        if (bean.getRead_num() != null) {
            sweepReadTimes.setText(bean.getRead_num());
        }
        // 被送花数
        if (bean.getFlower_num() != null) {
            flowerCount.setText(bean.getFlower_num());
        }
        // 被评论数
        if (bean.getComment_times() != null) {
            commentCount.setText(bean.getComment_times());
        }
        // 勋章
        if (bean.getUser() != null && bean.getUser().getMedal() != null) {
            DiaobaoUtil.setMedalImgView(bean.getUser().getMedal(), mUserMedal);
        }
        if (bean.getUser() != null && AccountService.getInstance().isUserSelf(bean.getUser().getUserId())) {
            mDeleteBtn.setVisibility(View.VISIBLE);
        } else {
            mDeleteBtn.setVisibility(View.GONE);
        }

        if (bean != null && bean.getAdd_time() != null) {
            mCreateTime.setText(DiaobaoUtil.seconds2TimeString(Long.parseLong(bean.getAdd_time())));
        }
    }


    @Override
    public void setHandler(Handler handler) {
        this.mHandler = handler;
    }

    @Override
    public BaseAdapterViewHolder createViewHolder(Context context, int position) {

        return new NovelReviewMessageViewHolder(context, position);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.item_sweep_click_btn:
                SimpleWeexActivity.startActivity(mContext,"vue/articalComment/TFArticalComment.js?novelId="+novelBean.getId());
                break;
            case R.id.home_item_sweep_userimg:
                StartActivityUtil.enterOtherPersonalCenter(mContext, bean.getUser().getUserId());
                break;
            case R.id.delete_item_btn:
                if (AccountService.getInstance().isUserSelf(bean.getUser().getUserId())) {
                    Message message = new Message();
                    message.what = BaseAdapterViewHolder.DELETE_NOVEL_REVIEW_ITEM;
                    Bundle bundle = new Bundle();
                    bundle.putString("id", bean.getId());
                    bundle.putInt("position", mPositon);

                    message.setData(bundle);
                    if (mHandler != null) {
                        mHandler.sendMessage(message);
                    }
                }
                break;
            default:
                break;
        }

    }

    @Override
    public boolean onLongClick(View view) {
        switch (view.getId()) {
            case R.id.item_sweep_click_btn:
                if (AccountService.getInstance().isUserSelf(bean.getUser().getUserId())) {
                    Message message = new Message();
                    message.what = BaseAdapterViewHolder.DELETE_NOVEL_REVIEW_ITEM;
                    Bundle bundle = new Bundle();
                    bundle.putString("id", bean.getId());
                    bundle.putInt("position", mPositon);

                    message.setData(bundle);
                    if (mHandler != null) {
                        mHandler.sendMessage(message);
                    }
                }
                break;
        }
        return false;
    }
}
